package org.jsola.hr.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IApplyToProgDAO;
import org.jsola.hr.dto.*;
import org.jsola.hr.entity.*;
import org.jsola.hr.query.ApplyToProgQuery;
import org.jsola.hr.service.*;
import org.jsola.hr.vo.ApplyToProgDepVO;
import org.jsola.hr.vo.ApplyToProgListVO;
import org.jsola.hr.vo.ApplyToProgVO;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrApplyToProgServiceImpl")
public class ApplyToProgServiceImpl implements IApplyToProgService {

    @Autowired
    private IApplyToProgDAO applyToProgDAO;
    @Autowired
    private IApplyToProgDepService applyToProgDepService;
    @Autowired
    private IApplyToProgLevelService applyToProgLevelService;
    @Autowired
    private IApplyToProgPositionService applyToProgPositionService;
    @Autowired
    private IEmpDepService empDepService;
    @Autowired
    private IEmpPositionService empPositionService;
    @Autowired
    private IEmpInfoService empInfoService;


    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ApplyToProgVO save(ApplyToProgAddDTO applyToProgAddDTO, TokenUser tokenUser) {

        // 转DO
        ApplyToProgDO applyToProgDO = applyToProgAddDTO.to(ApplyToProgDO.class);
        // 保存
        applyToProgDO = save(applyToProgDO, tokenUser.getSiteId(), tokenUser.getUserId());
        String progId = applyToProgDO.getId();

        applyToProgDepService.insertList(applyToProgAddDTO.getApplyToProgDepAddDTOList(), progId, tokenUser);
        applyToProgPositionService.insertList(applyToProgAddDTO.getApplyToProgPositionAddDTOList(), progId, tokenUser);
//        applyToProgLevelService.insertList(applyToProgAddDTO.getApplyToProgLevelAddDTOList(),progId,tokenUser);
        HrLogKit.logForSave("新增方案适用设置", "saveApplyToProg", applyToProgDO.getId(), tokenUser);
        return applyToProgDO.to(ApplyToProgVO.class);
    }


    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Object add(ApplyToProgAddDTO applyToProgAddDTO, TokenUser tokenUser) {

        //获取当前方案将要适用的员工
        List<ApplyToProgDepAddDTO> applyToProgDepAddDTOList = applyToProgAddDTO.getApplyToProgDepAddDTOList();
        List<ApplyToProgPositionAddDTO> applyToProgPositionAddDTOList = applyToProgAddDTO.getApplyToProgPositionAddDTOList();
        if (CollectionUtil.isNotEmpty(applyToProgDepAddDTOList) || CollectionUtil.isNotEmpty(applyToProgPositionAddDTOList)) {

            //当方案和方案之间有冲突时
            if (applyToProgAddDTO.getMutex()) {
                List<String> depEmpIds = new ArrayList<>();
                List<String> depIds = new ArrayList<>();
                if (!CollectionUtils.isEmpty(applyToProgDepAddDTOList)) {
                    applyToProgDepAddDTOList.forEach(applyToProgDepAddDTO -> {
                        if (applyToProgDepAddDTO.getType() == 2) {
                            depEmpIds.add(applyToProgDepAddDTO.getDepId());
                        } else {
                            depIds.add(applyToProgDepAddDTO.getDepId());
                        }
                    });
                }

                List<String> posIds = new ArrayList<>();
                if (!CollectionUtils.isEmpty(applyToProgPositionAddDTOList)) {
                    applyToProgPositionAddDTOList.forEach(a -> posIds.add(a.getPositionId()));
                }

                if ((depIds != null && depIds.size() > 0) || (posIds != null && posIds.size() > 0)) {
                    List list = this.validateConflictedSelectCondition(applyToProgAddDTO.getEmployType(), depIds, posIds, tokenUser.getSiteId(), applyToProgAddDTO.getBusProgType(), applyToProgAddDTO.getBusProgId(), applyToProgAddDTO.getCompanyId());
                    if (list != null && list.size() > 0) {
                        return list;
                    }
                }
                String empRange = applyToProgAddDTO.getEmployRange();
                //部门和岗位之间会生效的员工
                List<String> empIds = this.validateEmpValid(empRange.contains("1"), depEmpIds, empRange.contains("3"), posIds, false, null, tokenUser.getSiteId(), applyToProgAddDTO.getCompanyId());
                if (CollectionUtil.isNotEmpty(empIds)) {

                    //判断将要适用的员工是否和别的方案的员工冲突哦
                    List<EmpInfoVO> empInfoVOList = this.validateConflicted(empIds, applyToProgAddDTO.getBusProgId(), applyToProgAddDTO.getBusProgType(), tokenUser.getSiteId(), applyToProgAddDTO.getCompanyId());
                    if (empInfoVOList != null && empInfoVOList.size() > 0) {
                        return empInfoVOList;
                    }
                }
            }
        }
        // 转DO
        ApplyToProgDO applyToProgDO = applyToProgAddDTO.to(ApplyToProgDO.class);
        // 保存
        applyToProgDO = save(applyToProgDO, tokenUser.getSiteId(), tokenUser.getUserId());
        String progId = applyToProgDO.getId();
        if (CollectionUtil.isNotEmpty(applyToProgDepAddDTOList)) {
            applyToProgDepService.insertList(applyToProgDepAddDTOList, progId, tokenUser);
        }
        if (CollectionUtil.isNotEmpty(applyToProgPositionAddDTOList)) {
            applyToProgPositionService.insertList(applyToProgPositionAddDTOList, progId, tokenUser);
        }


//        applyToProgLevelService.insertList(applyToProgAddDTO.getApplyToProgLevelAddDTOList(),progId,tokenUser);
        return applyToProgDO.to(ApplyToProgVO.class);
    }

    /**
     * 判断当前方案所选条件是否已包含在其它相同业务类型方案中
     *
     * @param employType  适用员工类型（6：正式，1：实习，2：劳务，3：顾问，4：返聘，5：外包）
     * @param depIds      部门ids
     * @param posIds      岗位ids
     * @param siteId      站点id
     * @param busProgType 业务方案类型
     * @param busProgId   业务方案id
     * @return 已存在的方案
     */
    private List<ApplyToProgVO> validateConflictedSelectCondition(String employType, List<String> depIds, List<String> posIds, String siteId, String busProgType, String busProgId, String companyId) {
        List<String> employTypeList = new ArrayList<>();
        if (employType.contains("1")) {
            employTypeList.add("1");
        } else if (employType.contains("2")) {
            employTypeList.add("2");
        } else if (employType.contains("3")) {
            employTypeList.add("3");
        } else if (employType.contains("4")) {
            employTypeList.add("4");
        } else if (employType.contains("5")) {
            employTypeList.add("5");
        } else if (employType.contains("6")) {
            employTypeList.add("6");
        }


        return applyToProgDAO.getProgConflictSelectCondition(depIds, posIds, employTypeList, siteId, busProgType, busProgId, companyId);
    }


    @Override
    public List<EmpInfoVO> validateConflicted(List<String> empIds, String busProgId, String busProgType, String siteId, String companyId) {
        List<ApplyToProgEmpeeInfoDTO> list = this.getApplyToProgEmployee(busProgType, siteId, companyId);
        List<EmpInfoVO> conflictedEmp = new ArrayList<>();
        for (ApplyToProgEmpeeInfoDTO obj : list) {
            if (!obj.getBusProgId().equals(busProgId)) {
                List<EmpInfoDO> empInfoDOList = obj.getEmpInfoDOList();
                empInfoDOList.forEach(emp -> {
                    if (empIds.contains(emp.getId())) {
                        conflictedEmp.add(emp.to(EmpInfoVO.class));
                    }
                });

            }
        }
        return conflictedEmp;
    }

    @Override
    public List<String> validateEmpValid(Boolean isDepValid, List<String> depEmpIds, Boolean isPosValid, List<String> posIds,
                                         Boolean isLevelValid, List<String> levelIds, String siteId, String companyId) {
        //满足多个条件取交集
        List<String> empIds = new ArrayList<>();
        //判断empids是否直接取当前员工的值
        boolean isMixed = false;
        if (isDepValid) {
            empIds = depEmpIds != null ? depEmpIds : new ArrayList<>();
            isMixed = true;
        }
        if (isPosValid) {
            Example posExample = new Example(EmpPositionDO.class);
            posExample.and().andEqualTo("companyId", companyId);
            posExample.and().andEqualTo("siteId", siteId);
            List<EmpPositionDO> empPositionDOList = empPositionService.selectByExample(posExample);
            List<String> posEmpIds = new ArrayList<>();
            empPositionDOList.forEach(empPositionDO -> posEmpIds.add(empPositionDO.getEmpId()));
            if (isMixed) {
                empIds.retainAll(posEmpIds);
            } else {
                empIds = posEmpIds;
            }
        }
        return empIds;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Object update(ApplyToProgUpdateDTO applyToProgUpdateDTO, TokenUser tokenUser) {

        String siteId = tokenUser.getSiteId();

        //获取当前方案将要适用的员工
        List<ApplyToProgDepAddDTO> applyToProgDepAddDTOList = applyToProgUpdateDTO.getApplyToProgDepAddDTOList();
        List<ApplyToProgPositionAddDTO> applyToProgPositionAddDTOList = applyToProgUpdateDTO.getApplyToProgPositionAddDTOList();


        ApplyToProgDO applyToProgDOParam = new ApplyToProgDO();
        applyToProgDOParam.setBusProgType(applyToProgUpdateDTO.getBusProgType());
        applyToProgDOParam.setBusProgId(applyToProgUpdateDTO.getBusProgId());
        applyToProgDOParam.setSiteId(siteId);
        applyToProgDOParam = applyToProgDAO.selectOne(applyToProgDOParam);
        if (applyToProgDOParam == null) {
            throw new ParamException("查无此适用方案");
        }

        if (CollectionUtil.isNotEmpty(applyToProgDepAddDTOList) || CollectionUtil.isNotEmpty(applyToProgPositionAddDTOList)) {
            //当方案和方案之间有冲突时
            if (applyToProgUpdateDTO.getMutex()) {
                List<String> depEmpIds = new ArrayList<>();
                List<String> depIds = new ArrayList<>();
                applyToProgDepAddDTOList.forEach(applyToProgDepAddDTO -> {
                    if (applyToProgDepAddDTO.getType() == 2) {
                        depEmpIds.add(applyToProgDepAddDTO.getDepId());
                    } else {
                        depIds.add(applyToProgDepAddDTO.getDepId());
                    }
                });


                List<String> posIds = new ArrayList<>();
                applyToProgPositionAddDTOList.forEach(a -> posIds.add(a.getPositionId()));
                if ((depIds != null && depIds.size() > 0) || (posIds != null && posIds.size() > 0)) {
                    List list = this.validateConflictedSelectCondition(applyToProgUpdateDTO.getEmployType(), depIds, posIds, tokenUser.getSiteId(), applyToProgUpdateDTO.getBusProgType(), applyToProgUpdateDTO.getBusProgId(), applyToProgUpdateDTO.getCompanyId());
                    if (list != null && list.size() > 0) {
                        return list;
                    }
                }
                String empRange = applyToProgUpdateDTO.getEmployRange();
                //部门和岗位之间会生效的员工
                List<String> empIds = this.validateEmpValid(empRange.contains("1"), depEmpIds, empRange.contains("3"), posIds, false, null, tokenUser.getSiteId(), applyToProgUpdateDTO.getCompanyId());
                if (CollectionUtil.isNotEmpty(empIds)) {

                    //判断将要适用的员工是否和别的方案的员工冲突哦
                    List<EmpInfoVO> empInfoVOList = this.validateConflicted(empIds, applyToProgUpdateDTO.getBusProgId(), applyToProgUpdateDTO.getBusProgType(), tokenUser.getSiteId(), applyToProgUpdateDTO.getCompanyId());
                    if (empInfoVOList != null && empInfoVOList.size() > 0) {
                        return empInfoVOList;
                    }
                }
            }

        }
        String progId = applyToProgDOParam.getId();
        applyToProgLevelService.deleteByProgId(progId, tokenUser);
        applyToProgPositionService.deleteByProgId(progId, tokenUser);
        applyToProgDepService.deleteByProgId(progId, tokenUser);
        if (CollectionUtil.isNotEmpty(applyToProgDepAddDTOList)) {
            applyToProgDepService.insertList(applyToProgDepAddDTOList, progId, tokenUser);
        }
        if (CollectionUtil.isNotEmpty(applyToProgPositionAddDTOList)) {
            applyToProgPositionService.insertList(applyToProgPositionAddDTOList, progId, tokenUser);
        }
//        applyToProgLevelService.insertList(applyToProgUpdateDTO.getApplyToProgLevelAddDTOList(),progId,tokenUser);
        //转DO
        applyToProgUpdateDTO.setId(progId);
        ApplyToProgDO applyToProgDO = applyToProgUpdateDTO.to(ApplyToProgDO.class);


        HrLogKit.logForUpdate("修改方案适用设置", "updateApplyToProg", applyToProgDO.getId(), tokenUser);
        //根据主键更新，只更新非null值
        return updateByIdSelective(applyToProgDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... applyToProgIds) {
        HrLogKit.logForDelete("批量删除方案适用设置", "deleteApplyToProgByIds", Arrays.toString(applyToProgIds), tokenUser);
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) applyToProgIds);
    }


    @Override
    public ApplyToProgVO selectById(String applyToProgId, String siteId) {
        ApplyToProgDO applyToProgDO = selectDOById(applyToProgId, siteId);
        if (applyToProgDO == null) {
            return null;
        }
        return applyToProgDO.to(ApplyToProgVO.class);
    }

    @Override
    public ApplyToProgVO selectByBusProgId(String busProgId, String siteId) {
        ApplyToProgVO applyToProgVO = applyToProgDAO.selectByBusProgId(busProgId, siteId);
        List<ApplyToProgDepVO> applyToProgDepVOS = applyToProgVO.getApplyToProgDepVOList();
        if (CollectionUtils.isEmpty(applyToProgDepVOS)) {
            applyToProgDepVOS.forEach(applyToProgDepVO -> {
                if (applyToProgDepVO.getType() == 1) {
                    applyToProgDepVO.setEmpName(applyToProgDepVO.getDepName());
                }
            });
        }
        return applyToProgVO;
    }

    @Override
    public List<ApplyToProgListVO> select(ApplyToProgQuery applyToProgQuery, String siteId) {
        List<ApplyToProgDO> applyToProgDOList = selectDO(applyToProgQuery, siteId);
        if (CollectionUtils.isEmpty(applyToProgDOList)) {
            return applyToProgDOList == null ? null : new ArrayList<>();
        }
        return applyToProgDOList.stream()
                .map(applyToProgDO -> applyToProgDO.to(ApplyToProgListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ApplyToProgQuery applyToProgQuery, String siteId) {
        Example example = buildExample(applyToProgQuery, siteId);
        return applyToProgDAO.selectCountByExample(example);
    }

    @Override
    public Page<ApplyToProgListVO> selectPage(ApplyToProgQuery applyToProgQuery, String siteId) {
        Example example = buildExample(applyToProgQuery, siteId);
        Page<ApplyToProgDO> page = applyToProgDAO.selectPageByExample(example,
                applyToProgQuery.getPageNo(),
                applyToProgQuery.getPageSize());

        return page.to(ApplyToProgListVO.class);
    }

    @Override
    public ApplyToProgDO selectDOById(String applyToProgId, String siteId) {
        return listById(applyToProgId, siteId);
    }

    @Override
    public List<ApplyToProgDO> selectDO(ApplyToProgQuery applyToProgQuery, String siteId) {
        Example example = buildExample(applyToProgQuery, siteId);
        return applyToProgDAO.selectByExample(example);
    }


    @Override
    public List<ApplyToProgEmpeeInfoDTO> getApplyToProgEmployee(String busProgType, String siteId, String companyId) {
        ApplyToProgDO applyToProgDO = new ApplyToProgDO();
        applyToProgDO.setBusProgType(busProgType);
        applyToProgDO.setSiteId(siteId);
        applyToProgDO.setCompanyId(companyId);
        List<ApplyToProgDO> list = applyToProgDAO.select(applyToProgDO);
        List<ApplyToProgEmpeeInfoDTO> depEmp = applyToProgDAO.getApplyToProgDepEmp(busProgType, siteId, companyId);
        List<ApplyToProgEmpeeInfoDTO> selectEmp = applyToProgDAO.getApplyToProgEmp(busProgType, siteId, companyId);
        List<ApplyToProgEmpeeInfoDTO> posEmp = applyToProgDAO.getApplyToProgPosEmp(busProgType, siteId, companyId);
        List<ApplyToProgEmpeeInfoDTO> employeeTypeEmp = applyToProgDAO.getApplyToProgEmployeeTypeEmp(busProgType, siteId, companyId);
        List<ApplyToProgEmpeeInfoDTO> result = new ArrayList<>();
        //循环遍历获取适用员工
        List<EmpInfoDO> allSelectEmp = new ArrayList<>();
        for (ApplyToProgDO obj : list) {
            ApplyToProgEmpeeInfoDTO applyToProgEmpeeInfoDTO = new ApplyToProgEmpeeInfoDTO();
            applyToProgEmpeeInfoDTO.setBusProgId(obj.getBusProgId());
            //分别获取符合选中岗位的员工和直接选中的员工取交集
            Optional depOptional = depEmp.stream().filter(emp -> emp.getBusProgId().equals(obj.getBusProgId())).findFirst();
            ApplyToProgEmpeeInfoDTO depObj = depOptional.isPresent() ? (ApplyToProgEmpeeInfoDTO) depOptional.get() : new ApplyToProgEmpeeInfoDTO();

            Optional posOptional = posEmp.stream().filter(emp -> emp.getBusProgId().equals(obj.getBusProgId())).findFirst();
            ApplyToProgEmpeeInfoDTO posObj = posOptional.isPresent() ? (ApplyToProgEmpeeInfoDTO) posOptional.get() : new ApplyToProgEmpeeInfoDTO();

            Optional selectOptional = selectEmp.stream().filter(emp -> (emp.getBusProgId()).equals(obj.getBusProgId())).findFirst();
            ApplyToProgEmpeeInfoDTO selectObj = selectOptional.isPresent() ? (ApplyToProgEmpeeInfoDTO) selectOptional.get() : new ApplyToProgEmpeeInfoDTO();

            Optional employeeTypeOption = employeeTypeEmp.stream().filter(emp -> (emp.getBusProgId()).equals(obj.getBusProgId())).findFirst();
            ApplyToProgEmpeeInfoDTO employeeTypeObj = employeeTypeOption.isPresent() ? (ApplyToProgEmpeeInfoDTO) employeeTypeOption.get() : new ApplyToProgEmpeeInfoDTO();


            List<EmpInfoDO> posEmpList = posObj.getEmpInfoDOList() != null ? posObj.getEmpInfoDOList() : new ArrayList<>();
            ;
            List<EmpInfoDO> selectEmpList = selectObj.getEmpInfoDOList() != null ? selectObj.getEmpInfoDOList() : new ArrayList<>();
            List<EmpInfoDO> employeeTypeEmpList = employeeTypeObj.getEmpInfoDOList() != null ? employeeTypeObj.getEmpInfoDOList() : new ArrayList<>();
            List<EmpInfoDO> depEmployeeEmpList = depObj.getEmpInfoDOList() != null ? depObj.getEmpInfoDOList() : new ArrayList<>();

            String employRange = obj.getEmployRange();
            boolean isMixed = false;
            List empInfoRes = new ArrayList();
            List depInfoRes = new ArrayList();
            if (employRange.contains("1")) {
                empInfoRes = selectEmpList;
                depInfoRes = depEmployeeEmpList;
                isMixed = true;
            } else if (employRange.contains("3")) {
                if (isMixed) {
                    empInfoRes.retainAll(posEmpList);
                    depInfoRes.retainAll(posEmpList);
                } else {
                    empInfoRes = posEmpList;
                    isMixed = true;
                }
            }
            //岗位和部门适用员工所得结果和聘用形式员工取交集
            empInfoRes.retainAll(employeeTypeEmpList);
            depInfoRes.retainAll(employeeTypeEmpList);

            allSelectEmp.addAll(empInfoRes);
            applyToProgEmpeeInfoDTO.setEmpInfoDOList(empInfoRes);
            applyToProgEmpeeInfoDTO.setDepEmpInfoDOlist(depInfoRes);
            result.add(applyToProgEmpeeInfoDTO);
        }
        result.forEach(obj -> {
            List<EmpInfoDO> empInfoDOList = obj.getDepEmpInfoDOlist();
            empInfoDOList.removeAll(allSelectEmp);
            obj.getEmpInfoDOList().addAll(empInfoDOList);
        });
        return result;
    }

    @Override
    public ApplyToProgEmpeeInfoDTO getApplyToProgEmployeeByBusProgId(String busProgId, String siteId, String companyId) {
        ApplyToProgVO applyToProgVO = applyToProgDAO.selectByBusProgId(busProgId, siteId);
        List<ApplyToProgDepVO> applyToProgDepVOS = applyToProgVO.getApplyToProgDepVOList();
        if (CollectionUtils.isEmpty(applyToProgDepVOS)) {
            applyToProgDepVOS.forEach(applyToProgDepVO -> {
                if (applyToProgDepVO.getType() == 1) {
                    applyToProgDepVO.setEmpName(applyToProgDepVO.getDepName());
                }
            });
        }
        if (applyToProgVO == null) {
            return null;
        }
        String busProgType = applyToProgVO.getBusProgType();
        List<ApplyToProgEmpeeInfoDTO> list = this.getApplyToProgEmployee(busProgType, siteId, companyId);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        return list.stream().filter(a -> a.getBusProgId().equals(busProgId)).findFirst().orElse(null);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param applyToProgQuery 查询参数
     * @param siteId           所属站点id
     * @return example
     */
    private Example buildExample(ApplyToProgQuery applyToProgQuery, String siteId) {
        Example example = new Example(ApplyToProgDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (applyToProgQuery.getBusProgType() != null) {
            example.and().andLike("busProgType", "%" + applyToProgQuery.getBusProgType() + "%");
        }
        if (applyToProgQuery.getBusProgId() != null) {
            example.and().andLike("busProgId", "%" + applyToProgQuery.getBusProgId() + "%");
        }
        if (applyToProgQuery.getEmployType() != null) {
            example.and().andLike("employType", "%" + applyToProgQuery.getEmployType() + "%");
        }
        if (applyToProgQuery.getEmployRange() != null) {
            example.and().andLike("employRange", "%" + applyToProgQuery.getEmployRange() + "%");
        }
        if (applyToProgQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", applyToProgQuery.getCompanyId());
        }
        if (applyToProgQuery.getGmtCreateStart() != null &&
                applyToProgQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", applyToProgQuery.getGmtCreateStart(),
                    applyToProgQuery.getGmtCreateEnd());
        }
        if (applyToProgQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + applyToProgQuery.getCreateUserId() + "%");
        }
        if (applyToProgQuery.getGmtModifiedStart() != null &&
                applyToProgQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", applyToProgQuery.getGmtModifiedStart(),
                    applyToProgQuery.getGmtModifiedEnd());
        }
        if (applyToProgQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + applyToProgQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example, applyToProgQuery.getOrders());
        return example;
    }
}




